<template>
  <div>
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        required
        name="mobile"
        label="手机号码"
        placeholder="请输入手机号码"
        :rules="[
          { required: true },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对',
          },
        ]"
      />
      <van-field
        v-model="code"
        required
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="[
          { required: true},
          { pattern: /^\d{6}$/, message: '验证码格式不对' },
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
        :loading='isShow'
        :disabled='isShow'
        loading-text="登录中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import {userLogin} from './user.js'
import {setToken} from '@/utils/token'
export default {
  name: "login",
  data() {
    return {
      mobile: "",
      code: "246810",
      isShow:false
    };
  },
  methods: {
    onSubmit(values) {
      this.isShow = true
      userLogin(values).then(res => {
        // console.log(res);
        this.$toast.success("登录成功")
        setToken(res.data.data)
        this.$router.push('/')
      }).catch(err => {
        this.$toast.fail("登录失败")
      }).then(() =>{
        this.isShow = false
      })
    },
  },
};
</script>

<style lang='less'>
.van-nav-bar__content {
  background-color: #0098fa;
  .van-nav-bar__title {
    color: #fff;
  }
  .van-nav-bar__left {
    .van-nav-bar__text {
      color: #fff;
    }
    .van-icon {
      color: #fff;
    }
  }
}
</style>